package com.zzq.my.jetpacks.jectpackapp.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;

/**
 * 圆形进度条控件
 * 
 * @author zhangjun
 */
public class CircleProgressView2 extends View {

	protected static final String TAG = "CircleProgressView2";

	/** 屏幕密度(dip) */
	private float mDensity;

	/** 文本缩放密度(sp) */
	private float mScaledDensity;

	/** 控件宽度 */
	private int mWidth;

	/** 控件高度 */
	private int mHeight;

	/** 绘制圆弧的度数 */
	private float mDegrees;

	/** 圆弧宽度 */
	private float arcWidth;

	/** 圆弧与控件边缘间距 */
	private float pading;

	/** 用于绘制圆弧的矩形 */
	private RectF mRectF = null;

	/** 画笔 */
	private Paint mPaint = null;

	/** 背景颜色 */
	private int bgColor;

	/** 进度百分比 */
	private float percent = 0;

	/** 文字大小 */
	private float textSize;

	/** 点大小 */
	private float pointSize;

	public CircleProgressView2(Context context) {

		super(context);

		init(context);

	}

	public CircleProgressView2(Context context, AttributeSet attrs) {

		super(context, attrs);

		init(context);

	}

	/**
	 * 初始化
	 */
	private void init(Context context) {

		getDensity(context);

		Log.d(TAG, "Density:" + mDensity + " mScaledDensity:" + mScaledDensity);

		// 设置默认背景颜色
		bgColor = Color.parseColor("#00000000");// #3290C8

		// 设置间距
		pading = 10 * mDensity;

		// 设置默认圆弧宽度
		arcWidth = 2.3f;

		// 设置默认文字大小
		textSize = 13f;

		// 设置默认点大小
		pointSize = 3.5f;

		mPaint = new Paint();

		// 设置画笔抗锯齿
		mPaint.setAntiAlias(true);

	}

	@Override
	protected void onDraw(Canvas canvas) {

		getBaseValue();

		canvas.drawColor(bgColor);

		drawProgressArc(canvas);

		drawArcStartPoint(canvas);

		drawProgressText(canvas);

	}

	private void getBaseValue() {

		mWidth = getWidth();

		mHeight = getHeight();

	}

	/**
	 * 绘制圆弧
	 */
	private void drawProgressArc(Canvas canvas) {

		mPaint.setStyle(Style.STROKE);

		// 设置圆弧颜色
		mPaint.setColor(Color.WHITE);

		// 设置圆弧宽度
		mPaint.setStrokeWidth(arcWidth * mDensity);

		mRectF = new RectF(pading, pading, mWidth - pading, mHeight - pading);

		canvas.drawArc(mRectF, 270.0f, mDegrees, false, mPaint);

		mPaint.setColor(Color.parseColor("#66ffffff"));

		canvas.drawArc(mRectF, 270.0f + mDegrees, 360.0f - mDegrees, false,
				mPaint);

	}

	/**
	 * 绘制圆弧上起始点
	 */
	private void drawArcStartPoint(Canvas canvas) {

		Point point = measureArcStartPoint();

		mPaint.setStyle(Style.FILL);

		mPaint.setColor(0xFF3290C8);

		// 绘制背景色来实现弧线和起始点之间的间隙
		canvas.drawCircle(point.x, point.y, pointSize * 2 * mDensity, mPaint);

		mPaint.setColor(0x11FFFFFF);

		// 绘制点外围部分
		canvas.drawCircle(point.x, point.y, pointSize * 2 * mDensity, mPaint);

		mPaint.setColor(Color.WHITE);

		// 绘制点
		canvas.drawCircle(point.x, point.y, pointSize * mDensity, mPaint);

	}

	/**
	 * 计算圆弧起始点坐标
	 */
	private Point measureArcStartPoint() {

		// 圆弧半径
		float radius = mWidth / 2 - pading;

		Log.d(TAG, "radius:" + radius);

		Point point = new Point();

		if (mDegrees >= 0 && mDegrees < 90.0f) {

			point.x = (int) (mWidth / 2 + radius
					* Math.sin(Math.toRadians(mDegrees)));

			point.y = (int) (mHeight / 2 - radius
					* Math.cos(Math.toRadians(mDegrees)));

		} else if (mDegrees >= 90.0f && mDegrees < 180.0f) {

			float degree = mDegrees - 90.0f;

			point.x = (int) (mWidth / 2 + radius
					* Math.cos(Math.toRadians(degree)));

			point.y = (int) (mHeight / 2 + radius
					* Math.sin(Math.toRadians(degree)));

		} else if (mDegrees >= 180.0f && mDegrees < 270.0f) {

			float degree = mDegrees - 180.0f;

			point.x = (int) (mWidth / 2 - radius
					* Math.sin(Math.toRadians(degree)));

			point.y = (int) (mHeight / 2 + radius
					* Math.cos(Math.toRadians(degree)));

		} else if (mDegrees >= 270.0f && mDegrees <= 360.0f) {

			float degree = mDegrees - 270.0f;

			point.x = (int) (mWidth / 2 - radius
					* Math.cos(Math.toRadians(degree)));

			point.y = (int) (mHeight / 2 - radius
					* Math.sin(Math.toRadians(degree)));

		}

		return point;

	}

	/**
	 * 绘制文本
	 */
	private void drawProgressText(Canvas canvas) {

		String text = "已完成" + measureProgressText(percent) + "%";

		Paint paint = new Paint();

		paint.setAntiAlias(true);

		paint.setColor(Color.WHITE);

		paint.setTextSize(textSize * mScaledDensity);

		paint.setTextAlign(Align.CENTER);

		FontMetrics fontMetrics = paint.getFontMetrics();

		// 计算文字高度
		float fontHeight = fontMetrics.bottom - fontMetrics.top;

		// 计算文字baseline
		float textBaseY = mHeight - (mHeight - fontHeight) / 2
				- fontMetrics.bottom;

		canvas.drawText(text, mWidth / 2, textBaseY, paint);

	}

	/**
	 * 设置背景颜色
	 */
	@Override
	public void setBackgroundColor(int color) {

		this.bgColor = color;

		invalidate();

	}

	/**
	 * 设置圆环进度
	 * 
	 * @param percent
	 */
	public void setProgress(float percent) {

		if (percent > 1.0f) {

			percent = 1.0f;
		}

		this.percent = percent;

		mDegrees = 360.0f * percent;

		if (mDegrees > 360.0f) {

			mDegrees = 0.0f;
		}

		// 刷新
		invalidate();

	}

	/**
	 * 设置圆环宽度
	 */
	public void setArcWidth(float arcWidth) {

		this.arcWidth = arcWidth;

		invalidate();

	}

	/**
	 * 设置文字大小
	 */
	public void setProgressTextSize(float textSize) {

		this.textSize = textSize;

		invalidate();

	}

	/**
	 * 设置点大小
	 */
	public void setPointSize(float pointSize) {

		this.pointSize = pointSize;

		invalidate();

	}

	/**
	 * 计算文本进度
	 */
	private float measureProgressText(float progress) {

		return (float) Math.round(progress * 1000) / 10;

	}

	/**
	 * 获取屏幕密度和文本缩放密度
	 */
	private void getDensity(Context context) {

		DisplayMetrics metrics = new DisplayMetrics();

		((Activity) context).getWindowManager().getDefaultDisplay()
				.getMetrics(metrics);

		mDensity = metrics.density;

		mScaledDensity = metrics.scaledDensity;

	}

}
